package com.atguigu.srb.core.controller.admin;

import com.atguigu.common.exception.Assert;
import com.atguigu.common.result.R;
import com.atguigu.common.result.ResponseEnum;
import com.atguigu.srb.core.pojo.entity.IntegralGrade;
import com.atguigu.srb.core.service.IntegralGradeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author Alice
 * @since 2021-08-27 星期五 14:41
 */
@Api(tags = "积分等级管理")
@CrossOrigin
@RestController
@RequestMapping("/admin/core/integralGrade")
public class AdminIntegralGradeController {

  @Resource
  private IntegralGradeService integralGradeService;

  @ApiOperation("获取积分等级列表")
  @GetMapping("/list")
  public R listAll() {
    List<IntegralGrade> integralGradeList = integralGradeService.list();
    return R.ok().data("list", integralGradeList).message("获取积分等级列表成功");
  }

  @ApiOperation(value = "根据id获取积分等级记录")
  @GetMapping("/get/{id}")
  public R getById(@ApiParam(value = "要获取的数据的主键", example = "19", required = true) @PathVariable Long id) {
    IntegralGrade integralGrade = integralGradeService.getById(id);
    if (integralGrade != null) {
      return R.ok().data("object", integralGrade).message("获取成功");
    }
    return R.error().data("object", null).message("获取失败");
  }

  @ApiOperation(value = "根据id删除积分等级记录", notes = "逻辑删除数据")
  @DeleteMapping("/remove/{id}")
  public R removeById(@ApiParam(value = "要删除的数据的主键", example = "19", required = true) @PathVariable Long id) {
    boolean remove = integralGradeService.removeById(id);
    if (remove) {
      return R.ok().data("result", true).message("删除成功");
    }
    return R.error().data("result", false).message("删除失败");
  }

  @ApiOperation(value = "新增积分等级记录")
  @PostMapping("/save")
  public R save(@ApiParam(value = "积分等级对象", required = true) @RequestBody IntegralGrade integralGrade) {
    Assert.notNull(integralGrade.getBorrowAmount(), ResponseEnum.BORROW_AMOUNT_NULL_ERROR);
    boolean save = integralGradeService.save(integralGrade);
    if (save) {
      return R.ok().data("result", true).message("保存成功");
    }
    return R.ok().data("result", false).message("保存失败");
  }

  @ApiOperation(value = "根据id修改积分等级记录")
  @PutMapping("/update")
  public R updateById(@ApiParam(value = "积分等级对象", required = true) @RequestBody IntegralGrade integralGrade) {
    boolean save = integralGradeService.updateById(integralGrade);
    if (save) {
      return R.ok().data("result", true).message("修改成功");
    }
    return R.ok().data("result", false).message("修改失败");
  }

}
